package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.entity.Annunciate;
import com.qf.entity.User;
import com.qf.service.AnnounceService;
import com.qf.service.LogsService;
import com.qf.utils.AnnunciateExcel;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * 2019/12/10
 * qjh
 * springboot_oa_five
 * 面向对象面向君 不负代码不负卿
 */
@Controller
public class AnnounceController {

    @Autowired
    private AnnounceService announceService;
    @Autowired
    private LogsService logsService;

    //查询所有通告
    @RequestMapping("/resource/tonggao/demo1/list")
    @RequiresPermissions("announce:all")
    public String findAll(@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "")String annName, @RequestParam(defaultValue = "")String publishTimeBegin, @RequestParam(defaultValue = "")String publishTimeOver, ModelMap modelMap){
        PageInfo<Annunciate> all = announceService.findAllAnnounce(page, annName, publishTimeBegin, publishTimeOver);
        modelMap.addAttribute("all", all);
        modelMap.addAttribute("annName", annName);
        modelMap.addAttribute("publishTimeBegin",publishTimeBegin);
        modelMap.addAttribute("publishTimeOver",publishTimeOver);
        return "resource/tonggao/demo1/list";
    }

    //ajax修改状态
    @RequestMapping("/changeStatus")
    @ResponseBody
    public Integer changeStatus(Integer anId, Integer status,HttpSession session) throws IOException {
        Integer integer = announceService.updataStatus(anId, status,session);
//        User user = (User) session.getAttribute("user");
//        logsService.addLogs(user,annunciate);
        return integer;
    }

    //批量删除
    @RequestMapping("/bathdelete")
    public void bathDelete(int[] arr,String method,HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        if(method.equals("delete")){
            PrintWriter writer = response.getWriter();
            if(arr==null){
                writer.print("<script>alert('请选择！');location.href='/resource/tonggao/demo1/list'</script>");
                return;
            }
            Integer integer = announceService.bathDelete(arr);
            if(integer>0){
                writer.print("<script>alert('删除成功！');location.href='/resource/tonggao/demo1/list'</script>");
            }else{
                writer.print("<script>alert('删除失败！不能删除！');location.href='/resource/tonggao/demo1/list'</script>");
            }
        }else if(method.equals("daochu")){

            //导出数据
            //1.查询被选中的数据信息
            List<Annunciate> annunciate = announceService.findAnnunciateByArr(arr);
            //2.导出数据
            AnnunciateExcel.createFirst("用户信息");
            AnnunciateExcel.createOthers(annunciate);
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");
            String s = format.format(new Date());
            response.setContentType("application/x-download");
            String fileName=s+".xls";
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
            ServletOutputStream outputStream = response.getOutputStream();
            AnnunciateExcel.export(outputStream);

//                ExcelUtils.export(new FileOutputStream("f://"+(format.format(new Date()))+".xls"));
//            writer.print("<script>alert('导出成功');location.href='/resource/tonggao/demo1/list'</script>");

        }
    }

    //添加
    @RequestMapping("/annunciateadd")
    @RequiresRoles("系统管理员")
    public void annunciateAdd(Annunciate annunciate,HttpServletResponse response,HttpSession session) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        Integer integer = announceService.addAnnunciate(annunciate);
        if(integer>0){

            //记录日志
            User user = (User) session.getAttribute("user");
            logsService.addAnnunciate(user,annunciate);

            writer.print("<script>alert('添加成功！');location.href='/resource/tonggao/demo1/list'</script>");
        }else{
            writer.print("<script>alert('添加失败！');location.href='/resource/tonggao/demo1/list'</script>");
        }
    }

    //跳转添加页面
    @RequestMapping("/resource/tonggao/demo1/add")
    public String add(){
        return "resource/tonggao/demo1/add";
    }

    //查看
    @RequestMapping("/annunciatelook")
    public String lookAnnunciate(Integer anId,ModelMap modelMap){
        Annunciate annunciate = announceService.findAnnunciate(anId);
        modelMap.addAttribute("annunciate", annunciate);
        return "resource/tonggao/demo1/look";
    }

    //修改前查询
    @RequestMapping("/updatefind")
    public String updateFind(Integer anId, ModelMap modelMapy){
        Annunciate annunciate = announceService.findAnnunciate(anId);
        modelMapy.addAttribute("annunciate", annunciate);
        return "resource/tonggao/demo1/edit";
    }

    //修改
    @RequestMapping("/updateannunciate")
    public void updateAnnunciate(Annunciate annunciate, HttpServletResponse response, HttpSession session) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        Integer integer = announceService.updateAnnunciate(annunciate);
        if(integer>0){

            //记录日志
            User user = (User) session.getAttribute("user");
            logsService.addAnnunciate(user,annunciate);
            writer.print("<script>alert('修改成功！');location.href='/resource/tonggao/demo1/list'</script>");
        }else{
            writer.print("<script>alert('修改失败！');location.href='/resource/tonggao/demo1/list'</script>");
        }
    }
}

